Will Geary
wcg2111@columbia.edu
import pandas as pd
import numpy as np
raw_data = pd.read_csv('data/05012013.csv')
raw_data.head()
print len(np.unique(raw_data['Device'])), "Unique Device ID's in input dataset:"
print list(np.unique(raw_data['Device']))
import folium
import palettable
%matplotlib inline
Define the color palette.
color_palette = palettable.cmocean.sequential.Thermal_20
color_palette.show_discrete_image()
color_palette = color_palette.colors
Some helper functions for deaing with color from: http://www.psychocodes.in/rgb-to-hex-conversion-and-hex-to-rgb-conversion-in-python.html
def rgb2hex(r,g,b):
hex = "#{:02x}{:02x}{:02x}".format(r,g,b)
return hex
def hex2rgb(hexcode):
rgb = tuple(map(ord,hexcode[1:].decode('hex')))
return rgb
Function to create map of trips using Folium / Leaflet.
def map_trips(input_df, markers=False, zoom=8, colors = color_palette * 3, skip_devices=[], filename="output.html"):
df = input_df.copy()
# Drop nonsensical rows
df = df[~((df['Latitude'] == 90) & (df['Longitude'] == 180))]
df = df[~((df['Latitude'] == 0) & (df['Longitude'] == 0))]
# Drop devices in skip_devices list
df = df[~df['Device'].isin(skip_devices)]
# Mapbox basemap
baseurl = 'http://{s}.tiles.mapbox.com/v4/mapbox.light/{z}/{x}/{y}.png'
api_key = 'pk.eyJ1Ijoid2lsbGdlYXJ5IiwiYSI6ImNpdW9wcmkxNjAxbDUydXQ0MzFwdmdvOWkifQ.9YIdQhYTOpRpocyFK-tBNA'
token = '?access_token={}'.format(api_key)
# Initialize map
my_map = folium.Map(
location=[
np.mean(df['Latitude']),
np.mean(df['Longitude'])],
tiles = baseurl+token,
attr='Mapbox',
zoom_start=zoom)
unique_devices = np.unique(df['Device'])
number_of_unique_devices = len(unique_devices)
count = 0
# For every trip taken by every device...
# Create a polyline and add it to the map
for device_id in list(unique_devices):
data = df[df['Device'] == device_id]
for trip_id in list(np.unique(data['Trip'])):
trip = data[data['Trip'] == trip_id]
lats = [i for i in trip['Latitude']]
lons = [i for i in trip['Longitude']]
coordinates = zip(lats, lons)
#take every 10th coordinate
coordinates = coordinates[::10]
my_PolyLine=folium.PolyLine(
locations=coordinates,
weight=4,
color=rgb2hex(*colors[count]),
opacity=0.8)
my_map.add_child(my_PolyLine)
if markers == True:
my_marker = folium.Marker(location=[lats[0], lons[0]], popup='Device ID: {}'.format(device_id))#.add_to(map_4)
my_map.add_child(my_marker)
else:
pass
count += 1
my_map.save(filename)
return my_map
Here is what the raw data looks like.
map_trips(raw_data, zoom=7, filename="maps/raw_data.html")
Include markers to identify problematic device id's.
map_trips(raw_data, zoom=7, markers=True, filename="maps/raw_data_markers.html")